﻿using Sidvall.UI.Dialogs;
using Sidvall.UI.ViewModels;
using System.Threading.Tasks;

namespace Sidvall.UI.Uwp.Controls
{
    public sealed partial class ProgressControl
    {
        #region Public Members

        public event System.EventHandler ActivityCompleted;

        #region ViewModel

        public ProgressControlViewModel ViewModel
        {
            get
            {
                return (ProgressControlViewModel)this.DataContext;
            }
            set
            {
                var oldValue = this.DataContext as ProgressControlViewModel;
                if (oldValue != null)
                    oldValue.CloseSelected -= OnActivityCompleted;
                this.DataContext = value;
                if (value != null)
                    value.CloseSelected += OnActivityCompleted;

                this.DataContext = value;
            }
        }

        #endregion

        #region DoPrimaryActionAsync

        public async Task DoPrimaryActionAsync()
        {
            await this.ViewModel.ActionAsync();
        }

        #endregion
        #region InitializeContext

        public void InitializeContext(ProgressDialogContext context)
        {
            if (context == null)
                throw new System.ArgumentNullException(nameof(context));

            var item = new ProgressControlViewModel();
            item.Initialize(context);
            this.Context = context;
            this.ViewModel = item;
        }

        #endregion
        #region StartAsync

        public async Task StartAsync()
        {
            if (this.ViewModel.MonitoredTaskItem.Status == Sidvall.Tasks.TaskStatus.Created)
            {
                await this.ViewModel.MonitoredTaskItem.ExecuteAsync(this.Context.ExecuteTasksAsOne);
                if (this.ViewModel != null)
                {
                    switch (this.ViewModel.MonitoredTaskItem.Status)
                    {
                        case Tasks.TaskStatus.Faulted:
                            if (this.ViewModel.CloseWhenFaulted)
                                OnActivityCompleted();
                            break;
                        default:
                            if (this.ViewModel.CloseWhenFinished)
                                OnActivityCompleted();
                            break;
                    }
                }
            }
        }

        #endregion
        #region Stop

        public void Stop()
        {
            if (this.ViewModel != null)
            {
                this.ViewModel.MonitoredTaskItem.Cancel();
                this.Context.Status = this.ViewModel.MonitoredTaskItem.Status;
                this.Context.Exception = this.ViewModel.MonitoredTaskItem.Exception;
                this.ViewModel.Dispose();
                this.ViewModel = null;
            }
        }

        #endregion

        #endregion
        #region Private Members

        private ProgressDialogContext Context { get; set; }

        #region OnActivityCompleted

        private void OnActivityCompleted()
        {
            OnActivityCompleted(new System.EventArgs());
        }
        private void OnActivityCompleted(System.EventArgs args)
        {
            OnActivityCompleted(this, args);
        }
        private void OnActivityCompleted(object sender, System.EventArgs args)
        {
            if (this.ActivityCompleted != null)
                this.ActivityCompleted.Invoke(sender, args);
        }

        #endregion

        #endregion
        #region Constructors

        public ProgressControl()
        {
            this.InitializeComponent();
        }

        #endregion
    }
}
